package com.example.studentstatusmanagement.controller;

import com.example.studentstatusmanagement.config.SystemVariable;
import com.example.studentstatusmanagement.pojo.*;
import com.example.studentstatusmanagement.pojo.doo.ReqHygiene;
import com.example.studentstatusmanagement.pojo.doo.ReqRoom;
import com.example.studentstatusmanagement.pojo.dto.SHygiene;
import com.example.studentstatusmanagement.pojo.dto.SRoom;
import com.example.studentstatusmanagement.service.HygieneService;
import com.example.studentstatusmanagement.utils.ResultResponse;
import com.example.studentstatusmanagement.utils.ResultTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/v1/hygiene")
public class HygieneController {

    @Autowired
    private HygieneService hygieneService;

    @RequestMapping("show")
    public Map<String, Object> showHygieneInfo(ReqHygiene reqHygiene) {

        Map<String, Object> so = new HashMap<String, Object>();

        List<SHygiene> ls = hygieneService.selectStudentHygienes(reqHygiene);

        so.put("code", SystemVariable.URL_SUCCESS);
        so.put("msg", "");
        so.put("count", ls.size());
        so.put("data", ls);

        return so;
    }

    @RequestMapping("del")
    public ResultResponse del(Integer id) {

        if (hygieneService.deleteByPrimaryKey(id) == SystemVariable.SUCCESS) {
            return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("update")
    public ResultResponse update(Hygiene hygiene) {
        if (!StringUtils.isEmpty(hygiene.getId())) {
            if (hygieneService.updateByPrimaryKeySelective(hygiene) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("add")
    public ResultResponse add(Hygiene hygiene) {


        hygiene.setCreatetime(new Date());
        ReqHygiene hygiene1 = new ReqHygiene();
        hygiene1.setDid(hygiene.getDid());
        hygiene1.setIstoday("on");

        if (hygieneService.selectStudentHygienes(hygiene1).size() > 0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.TODAY_IS_HAVE);
        if (hygieneService.insertSelective(hygiene) == SystemVariable.SUCCESS) {
            return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
}
